package com.xinwei.excpetion;

import com.xinwei.common.ApiRestResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
    private static Logger log = LogManager.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handleException(Exception e){
        log.error("Default Exception : "+e);
        e.printStackTrace();
        return ApiRestResponse.error(ManagerExceptionEnmu.SYSTEM_ERROR);
    }

    @ExceptionHandler(ManagerException.class)
    @ResponseBody
    public Object handleBlogException(ManagerException e){
        log.error("BlogException : "+e);
        return ApiRestResponse.error(e.getCode(), e.getMessage());
    }
}
